package com.sch.www.sys.services.impl;

import com.sch.www.common.base.BaseServicesSimpleImp;
import com.sch.www.common.base.FeatureTreeRelation;
import com.sch.www.common.base.FeatureTreeServices;
import com.sch.www.sys.orm.dao.manual.ResourceMapper;
import com.sch.www.sys.orm.entity.manual.Resource;
import com.sch.www.sys.orm.entity.manual.ResourceExample;
import com.sch.www.sys.services.RoleToResourceServices;
import com.sch.www.sys.services.ResourceServices;
import com.sch.www.sys.services.RoleServices;
import com.sch.www.sys.services.AccountToRoleServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class ResourceServicesImpl extends BaseServicesSimpleImp<Resource, ResourceExample, ResourceMapper, ResourceServices> implements ResourceServices, FeatureTreeServices<Resource> {

    @Autowired
    RoleServices roleServices;

    @Autowired
    RoleToResourceServices roleToResourceServices;

    @Autowired
    AccountToRoleServices accountToRoleServices;

    @Override
    protected String getTableName() {
        return null;
    }

    @Override
    protected ResourceExample getExampleNewInstance() {
        return new ResourceExample();
    }

    @Override
    public List<String> selectIdByAccountId(String... accountIds) {
        List<String> roleIdList = roleServices.selectIdByAccountId(accountIds);
        List<String> resourceIdList = getCurrentProxy().selectIdByRoleId(roleIdList.toArray(new String[]{}));
        return resourceIdList;
        
    }

    @Override
    public List<Resource> selectByAccountId(String... accountIds) {
        List<String> roleIdList = roleServices.selectIdByAccountId(accountIds);
        List<Resource> resourceList = getCurrentProxy().selectByRoleId(roleIdList.toArray(new String[]{}));
        return resourceList;
    }

    @Override
    public List<String> selectIdByRoleId(String... roleIds) {
        List<String> resourceIdList = roleToResourceServices.selectResourceIdByRoleId(roleIds);
        return resourceIdList;
    }

    @Override
    public List<Resource> selectByRoleId(String... roleIds) {
        List<String> resourceIdList = getCurrentProxy().selectIdByRoleId(roleIds);
        List<Resource> resourceList = getCurrentProxy().selectById(resourceIdList);
        return resourceList;
    }


    @Override
    public List<Resource> selectTreeByAccountId(String... accountIds) {
        List<String> resourceIdList = getCurrentProxy().selectIdByAccountId(accountIds);
        Set<String> idTreeSet1 = getFeatureTreeRelation().getAncestors(resourceIdList, true);
        List<Resource> resourceList = getCurrentProxy().selectById(new ArrayList<>(idTreeSet1));
        return resourceList;

    }

    @Override
    public List<Resource> selectTreeByRoleId(String... roleIds) {
        List<String> resourceIdList = getCurrentProxy().selectIdByRoleId(roleIds);
        Set<String> idTreeSet1 = getFeatureTreeRelation().getAncestors(resourceIdList, true);
        List<Resource> resourceList = getCurrentProxy().selectById(idTreeSet1);
        return resourceList;
    }

    @Override
    public List<Resource> selectAll() {
        return getMapper().selectByExample(null);
    }

    @Override
    public FeatureTreeRelation getFeatureTreeRelation() {
        List<Resource> resourceList = getCurrentProxy().selectAll();
        return new FeatureTreeRelation(resourceList);

    }
}
